package com.animerank.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.animerank.dto.VoteDTO;
import com.animerank.entity.Anime;
import com.animerank.entity.Nomination;
import com.animerank.service.IAnimeService;
import com.animerank.service.INominationService;
import com.animerank.dto.RankDTO;
import com.animerank.dto.Result;
import com.animerank.service.IVoteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 动漫控制器
 */
@RestController
@RequestMapping("/anime")
@Tag(name = "动漫管理", description = "动漫搜索、排行榜、提名等接口")
public class AnimeController {

    @Autowired
    private IAnimeService animeService;

    @Autowired
    private INominationService nominationService;

    @Autowired
    private IVoteService voteService;

    /**
     * 搜索动漫接口
     */
    @GetMapping("/search")
    @Operation(summary = "搜索动漫", description = "根据关键词搜索动漫")
    public Result<List<Anime>> searchAnime(@RequestParam String keyword) {
        return animeService.searchAnime(keyword);
    }

    /**
     * 获取月度动漫排行榜（支持分页）
     */
    @GetMapping("/rank/monthly")
    @Operation(summary = "获取月度排行榜", description = "获取当月动漫投票排行榜，支持分页")
    public Result<Map<String, Object>> getMonthlyRank(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return animeService.getMonthlyRank(page, pageSize);
    }

    /**
     * 提名动漫接口
     */
    @PostMapping("/nominate/{animeId}")
    @SaCheckLogin
    @Operation(summary = "提名动漫", description = "用户提名动漫，需要登录")
    public Result<?> nominate(@PathVariable Long animeId, @RequestBody Anime anime, HttpServletRequest request) {
        // 确保anime对象的id与路径参数一致
        anime.setId(animeId);
        Result<?> nominate = nominationService.nominate(anime);
        // 获取用户IP地址
        String ip = request.getRemoteAddr();
        // 投一票
        VoteDTO voteDTO = new VoteDTO();
        voteDTO.setAnimeId(anime.getId());
        voteService.vote(voteDTO, ip);
        return nominate;
    }

    /**
     * 获取用户的提名记录（支持分页）
     */
    @GetMapping("/nominations")
    @SaCheckLogin
    @Operation(summary = "获取用户提名记录", description = "获取当前登录用户的提名记录")
    public Result<Map<String, Object>> getUserNominations(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "9") Integer pageSize) {
        return nominationService.getUserNominations(page, pageSize);
    }

    /**
     * 获取动漫详情接口
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取动漫详情", description = "根据ID获取动漫的详细信息")
    public Result<Anime> getAnimeDetail(@PathVariable Long id) {
        return animeService.getAnimeDetail(id);
    }

    /**
     * 获取动漫投票数接口
     */
    @GetMapping("/vote/count/{animeId}")
    @Operation(summary = "获取动漫投票数", description = "获取指定动漫的投票数量")
    public Result<Integer> getAnimeVoteCount(@PathVariable Long animeId) {
        return animeService.getAnimeVoteCount(animeId);
    }

    /**
     * 获取全部动漫接口（支持分页和筛选）
     */
    @GetMapping("/queryPage")
    @Operation(summary = "获取全部动漫", description = "获取所有动漫，支持分页、筛选和排序")
    public Result<Map<String, Object>> getAllAnimes(
            @RequestParam(required = false) String query,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "12") Integer pageSize,
            @RequestParam(required = false) String season,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false, defaultValue = "rating") String sortBy) {
        return animeService.getAllAnimes(query, page, pageSize, season, year, genre, sortBy);
    }

}